Skip to content

Fix drag selection stopping when scrolling beyond visible items#735

Merged
AlexV525 merged 7 commits into
mainfrom
copilot/fix-drag-select-bug
Dec 14, 2025
Merged

Fix drag selection stopping when scrolling beyond visible items#735
AlexV525 merged 7 commits into
mainfrom
copilot/fix-drag-select-bug

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Dec 7, 2025

Plan: Fix drag-to-select stopping when scrolling to second page

Problem Analysis

  • Understand the issue: Drag selection stops working when scrolling to the second page
  • Identify root cause: Gesture detectors are attached per-item in SliverChildBuilderDelegate, only working for mounted items
  • Review maintainer comments: Need to refactor to calculate positions globally without relying on item mounting state

Solution Approach

  • Move gesture detection from per-item to a global overlay/listener
  • Update AssetGridDragSelectionCoordinator to calculate target indices from global positions
  • Modify assetsGridBuilder to use a single gesture detector wrapper instead of per-item detectors
  • Fix Scrollable context issue - Get ScrollableState from scroll controller instead of BuildContext
  • Ensure auto-scrolling continues to work correctly
  • Test the changes with the example app (requires Flutter environment)

Implementation Steps

  • Create a wrapper widget for the entire grid with gesture detection
  • Update coordinator to handle selections purely based on calculated indices
  • Remove per-item gesture detectors
  • Fix assertion error by accessing ScrollableState from scroll controller's position
  • Verify drag selection works across multiple pages (pending Flutter environment)
  • Run existing tests to ensure no regressions (pending Flutter environment)

Changes Made

1. asset_grid_drag_selection_coordinator.dart

  • Added _calculateIndexFromPosition() method (lines 56-144) to calculate asset index from global position coordinates
  • Updated onSelectionStart() to:
    • Accept BoxConstraints instead of index and asset
    • Get ScrollableState from gridScrollController.position.context.notificationContext instead of Scrollable.maybeOf(context)
    • Handle case where auto-scroller may not be available gracefully
  • Removed _checkScrollableStatePresent() method as it's no longer needed
  • Refactored onSelectionUpdate() to use the shared _calculateIndexFromPosition() method

2. asset_picker_builder_delegate.dart

  • Wrapped the entire CustomScrollView with a single GestureDetector (lines 1498-1568)
  • Removed per-item gesture detectors from sliverGrid builder

Bug Fix (Latest Commit)

Issue: Assertion failure when drag gesture starts because Scrollable.maybeOf(context) returns null

  • The context passed to gesture handlers was from outside the CustomScrollView, so no Scrollable ancestor was found
  • Solution: Access ScrollableState directly from the ScrollController's position context instead of relying on BuildContext
  • This works because the scroll controller is attached to the CustomScrollView and has access to its internal context
Original prompt

This section details on the original issue you should resolve

<issue_title>[BUG] 拖拽选择到第二页会停止</issue_title>
<issue_description>Describe the bug
拖拽选择图片,滑动到第二页无法继续选择
https://github.com/user-attachments/assets/94d7241e-4987-4998-81a5-2b91d1ba80c0

How to reproduce
WeChat Assets Picker 示例

Version information

  • WeChat Assets Picker: [v9.5.0]
  • Flutter Version: [v3.24.1]
    </issue_description>

Comments on the Issue (you are @copilot in this section)

@AlexV525 This is limited by the current implementation of the auto-scroll gesture. It needs a refactor to determine scroll positions globally to get rid of the item mounting state. @AlexV525 > 感觉可以参考这个库的实现:[drag_select_grid_view](https://pub.dev/packages/drag_select_grid_view)

该项目相较于参考的库而言复杂度不在同一个等级,需要调研层级调整对 delegate 整体交互效果的影响。</comment_new>


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

Co-authored-by: AlexV525 <15884415+AlexV525@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix dragging selection issue on second page Fix drag selection stopping when scrolling beyond visible items Dec 7, 2025
Copilot AI requested a review from AlexV525 December 7, 2025 10:43
@AlexV525
Copy link
Copy Markdown
Member

@copilot When the drag gesture starts, the following assertion thrown:

The following assertion was thrown while handling a gesture:
The drag select feature must use along with scrollables.
'package:wechat_assets_picker/src/delegates/asset_grid_drag_selection_coordinator.dart':
Failed assertion: line 308 pos 7: 'scrollable != null'

When the exception was thrown, this was the stack: 
#2      AssetGridDragSelectionCoordinator._checkScrollableStatePresent (package:wechat_assets_picker/src/delegates/asset_grid_drag_selection_coordinator.dart:308:7)
#3      AssetGridDragSelectionCoordinator.onSelectionStart (package:wechat_assets_picker/src/delegates/asset_grid_drag_selection_coordinator.dart:62:29)
#4      DefaultAssetPickerBuilderDelegate.assetsGridBuilder.<anonymous closure>.<anonymous closure>.<anonymous closure>.<anonymous closure> (package:wechat_assets_picker/src/delegates/asset_picker_builder_delegate.dart:1504:49)

… controller

Co-authored-by: AlexV525 <15884415+AlexV525@users.noreply.github.com>
Copy link
Copy Markdown
Contributor Author

Copilot AI commented Dec 14, 2025

@copilot When the drag gesture starts, the following assertion thrown:

...

Fixed in commit 92b3372. The issue was that the context passed to gesture handlers was from outside the CustomScrollView, so Scrollable.maybeOf(context) couldn't find a Scrollable ancestor. Now accessing ScrollableState directly from the scroll controller's position context: scrollPosition.context.notificationContext.

@AlexV525 AlexV525 marked this pull request as ready for review December 14, 2025 16:56
@AlexV525 AlexV525 requested a review from LeGoffMael as a code owner December 14, 2025 16:56
@AlexV525 AlexV525 merged commit 607e61d into main Dec 14, 2025
7 checks passed
@AlexV525 AlexV525 deleted the copilot/fix-drag-select-bug branch December 14, 2025 17:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] 拖拽选择到第二页会停止

2 participants